package com.wavercaz.freeforum.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {
    private static final String SECRET = "TWE21VSD";

    public static String getToken(Map<String, Object> data) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 24);
        JWTCreator.Builder builder = JWT.create();
        builder.withExpiresAt(calendar.getTime());
        data.forEach((k, v) -> {
            builder.withClaim(k, v.toString());
        });
        return builder.sign(Algorithm.HMAC256(SECRET));
    }

    public static DecodedJWT verifyToken(String token) {
        Verification verification = JWT.require(Algorithm.HMAC256(SECRET));
        return verification.build().verify(token);
    }
}
